<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class IndexController extends \App\Http\Controllers\HomeController{
    private function orgData() {
        return "";
        $users = session('home');
        $msg = '';
        if (!$users->name || !$users->cardnumber) {
            $msg = "请先在个人中心完善信息";
        }
        return $msg;
    }
    /**
     * 新装产品
     */
    public function index(Request $request) {
        $msg = $this->orgData();
        $pcates = \App\Models\Pcate::orderBy("sort", 'asc')->get();
        $assign = compact('pcates', 'msg', 'request');
        return view('home.index.index', $assign);
    }
    
    /**
     * 产品简介
     */
    public function product(Request $request) {
        $msg = $this->orgData();
        $areas = \App\Models\Area::orderBy("sort")->get();
        $products = \App\Models\Product::where("status", 1)->where("area", $request->input("area", 1))->where('ptype', $request->input('ptype'))->where("pcateid", $request->input("pcate", 1))->orderBy('sort', 'asc')->get();
        $pcate = \App\Models\Pcate::select('name', 'rate')->find($request->input('pcate', 1));
        $assign = compact("areas", "products", 'pcate', 'request', 'msg');
        return view("home.index.product", $assign);
    }
    
    /**
     * 续费产品
     * @return type
     */
    public function renew (Request $request) {
        $products = \App\Models\Product::whereIn('status', [1])->whereIn('ptype', [2])->where('area', $request->input('area', 1))->get();
        $assign = compact('products');
        return view('home.index.renew', $assign);
    }
    /**
     * 产品详情付款页面
     * @param type $id
     * @return type
     */
    public function productShow(Request $request) {
        $err = '';
        if ($request->has('name')) {
            $result = \App\Models\Order::order($request);
            if ($result['status'] == 0) {
                $err = $result['info'];
            } else {
                return redirect("home/index/order/{$result['info']->id}");
            } 
        }
        $msg = $this->orgData();
        $products = \App\Models\Product::whereIn('status', [1])->where('ptype', $request->input('ptype'))->where("area", $request->input("area", 1))->where("pcateid", $request->input("pcate"))->orderBy('sort', 'asc')->get();
        $pcate = \App\Models\Pcate::select('name', 'rate')->find($request->input('pcate'));
        $plot = \App\Models\Plot::where("id", $request->input('plot'))->first();
        $assign = compact('products', 'plot', 'pcate', 'msg', 'request', 'err');
        return view('home.index.productshow', $assign);
    }
    public function orderPay($orderid) {
        $order = \App\Models\Order::where('id', $orderid)->where('uid', session('home')->id)->first();
        return view('home.index.order', ['order'=>$order]);
    }


    /**
     * 生成支付订单，进行代付流程
     * @param Request $request
     * @return type
     */
    public function dorderPay(Request $request) {
        if ($request->ajax()) {
            $res = \App\Models\Order::where('order', "{$request->input('order')}")->update(['payphone' => "{$request->input('payphone')}"]);
            if ($res) {
                return [
                    'status'    => 1,
                    'info'      => "申请代付成功"
                ];
            } else {
                return [
                   'status'    => 1,
                   'info'      => "申请代付失败请重新提交订单" 
                ];
            }
            
        } elseif($request && !$request->ajax()) {
            $result = \App\Models\Order::order($request);
            if ($result['status'] == 0) {
                return redirect('home/index/productshow/'.$request->input('productid'))->with('msg', [$result['info'], $request->all()]);
            } else {
                $result = $result['info'];
            }
        }
        $assign = compact('result');
        return view('home.index.payagent', $assign);
    }
    /**
     * 选择小区
     */
    public function plot(Request $request) {
        
        if ($request->ajax()) {
            if (!$request->has('plot') || !$request->has('area')) {
                return \Illuminate\Support\Facades\Response::json(array('status'=> 0, 'info'=> "小区或者是地区不完整"));
            }
            $plot = \App\Models\Plot::where('status', 1)->where('area', $request->input('area', 1))->where('name', 'like', "%{$request->input('plot')}%")->select('name', 'id')->get();
            return \Illuminate\Support\Facades\Response::json(array('status'=> 1, 'info'=> $plot));
        }
        
        $assign = compact('request');
        return view("home.index.plot", $assign);
    }
}
